<template>
  <div>
    <Card shadow>
      <Table border :columns="columns" :data="data"></Table>
    </Card>
  </div>
</template>

<script>
    export default {
        components: {
        },
        data () {
            return {
                searchURL: '/print',
                baseURL: '/user/finance/print',
                selectRows: [],
                data: [],
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '打印机名称',
                        key: 'printName'
                    },{
                        title: '是否默认',
                        key: 'isDefault',
                        render: (h, params) => {
                            let temp = params.row.isDefault
                            if (temp === 0) {
                                temp = '否'
                            } else if (temp === 1) {
                                temp = '是'
                            }
                            return h('span', temp)
                        }
                    },
                    {
                        title: '操作',
                        width: 200,
                        align: 'center',
                        render: (h, params) => {
                            var label = params.row.isDefault === 1 ? '默认打印机' : '设为默认'
                            var disabled = params.row.isDefault === 1 ? true : false
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'info',
                                        size: 'default',
                                        disabled: disabled
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.apply(params.index, params.row)
                                        }
                                    }
                                }, label)
                            ]);
                        }
                    }
                ]
            };
        },
        methods: {
            events (item) {

            },
            search () {
                var prints = []
                let url = this.searchURL + '/list'
                this.$print.get(url).then(response => {
                    let {code, data, msg} = response.data
                    if (code === 0) {
                        prints = data
                        this.$print.get('/print/getHost').then(response => {
                            let {code, data, msg} = response.data
                            if (code === 0) {
                                this.$ajax.get(this.baseURL + '/getDefault/' + data).then(response => {
                                    let {code, data, msg} = response.data
                                    if (code === 0) {
                                        if (data) {
                                            for (let print of prints) {
                                                if (print.printName === data.printName) {
                                                    print.isDefault = 1
                                                }
                                            }
                                            this.data = prints
                                        } else {
                                            this.data = prints
                                        }
                                    } else {
                                        this.data = prints
                                    }
                                })
                            } else {
                                this.$bus.alert('error', msg);
                            }
                        })
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            apply (index, row) {
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, row).then(response => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '设置成功')
                        this.search()
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.search()
            }
        },
        mounted () {
            this.init()
        },
        watch: {
            '$route' (to) {
                if (to.path === '/print') {
                    this.init()
                }
            }
        }
    }
</script>

<style>

</style>
